﻿package Component
{
	import Map.Block;
	import Map.BlazePool;
	import Map.BombPool;
	import flash.events.Event;
	import flashx.textLayout.formats.Direction;
	import Utils.Directions;

	public class Bomb extends Block
	{
		private var propagation_time:uint;
		private var map:Map;
		private var _blaze_pool:BlazePool;
		private var blazes:Vector.<Vector.<Blaze >  > ;
		public function get blaze_pool():BlazePool
		{
			return _blaze_pool;
		}

		public function set blaze_pool(_blaze_pool:BlazePool)
		{
			this._blaze_pool = _blaze_pool;
		}

		private var _bomb_pool:BombPool;
		public function get bomb_pool():BombPool
		{
			return _bomb_pool;
		}

		public function set bomb_pool(_bomb_pool:BombPool)
		{
			this._bomb_pool = _bomb_pool;
		}

		public function Bomb()
		{
			super();
		}

		public function attach(_bomb_pool:BombPool,_blaze_pool:BlazePool)
		{
			this._blaze_pool = _blaze_pool;
			this._bomb_pool = _bomb_pool;
			blazes = new Vector.<Vector.<Blaze>>();
			blazes.push(new Vector.<Blaze>());
			blazes.push(new Vector.<Blaze>());
			blazes.push(new Vector.<Blaze>());
			blazes.push(new Vector.<Blaze>());
			visible = true;
		}

		public function explode(length:uint)
		{
			var blaze:Blaze;
			for (var amplitude:int = 1; amplitude < length + 1; amplitude++)
			{
				// make blazes in all directions
				for (var direction:uint = 0; direction < 4; direction++)
				{
					blaze = _blaze_pool.grab();
					blaze.visible = true;
					blaze.speed = (amplitude * 20) / stage.frameRate;
					blaze.setPosition(pos_x,pos_y);
					blazes[direction].push(blaze);
				}
			}
			propagation_time = stage.frameRate;
			addEventListener(Event.ENTER_FRAME,propagate);
		}

		public function propagate(evt:Event)
		{
			if (propagation_time == 0)
			{
				removeEventListener(Event.ENTER_FRAME,propagate);
				// release blazes
				for (var index:uint = 0; index < blazes[0].length; index++)
				{
					blaze_pool.release(blazes[0][index]);
					blaze_pool.release(blazes[1][index]);
					blaze_pool.release(blazes[2][index]);
					blaze_pool.release(blazes[3][index]);
				}
				blazes[0].length = 0;
				blazes[1].length = 0;
				blazes[2].length = 0;
				blazes[3].length = 0;
				
				// we release the bomb
				bomb_pool.release(this);
				return;
			}
			// we move all blazes
			for (var index:uint = 0; index < blazes[0].length; index++)
			{
				blazes[0][index].up();
				blazes[1][index].down();
				blazes[2][index].right();
				blazes[3][index].left();
			}
			propagation_time--;
		}
		
		public override function onMapReset() {
			visible = false;
		}

		public function kick(direction:uint) {
			switch(direction) {
				case Directions.DOWN :
					setPosition(pos_x,pos_y + 1);
				break;
				case Directions.LEFT :
					setPosition(pos_x - 1,pos_y);
				break;
				case Directions.RIGHT :
					setPosition(pos_x + 1,pos_y);
				break;
				case Directions.UP :
					setPosition(pos_x,pos_y - 1);
				break;
			}
		}
	}
}